/**
 * 递归 - 镜像二叉树
 */
#include <iostream>
#include <vector>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right)
        : val(x), left(left), right(right) {}
};

class Solution {
public:
    bool check(TreeNode *p, TreeNode *q) {
        if (!p && !q) return true; // 两个都为空
        if (!p || !q) return false; // 一个为空
        return p->val == q->val && check(p->left, q->right) &&
               check(p->right, q->left);
    }

    bool isSymmetric(TreeNode *root) { return check(root, root); }
};

int main() {
    Solution s;

    // 一号树
    TreeNode *tree1 = new TreeNode(1);
    tree1->left = new TreeNode(5);
    tree1->right = new TreeNode(2);
    tree1->right->left = new TreeNode(3);
    cout << s.isSymmetric(tree1) << endl;

    // 二号树
    TreeNode *tree2 = new TreeNode(1);
    tree2->left = new TreeNode(2);
    tree2->right = new TreeNode(2);
    tree2->left->right = new TreeNode(3);
    tree2->right->left = new TreeNode(3);
    cout << s.isSymmetric(tree2) << endl;

    return 0;
}